package com.zrfintech.manage.controller.mg;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zrfintech.common.pojo.BasePageEntity;
import com.zrfintech.domain.MgMenu;
import com.zrfintech.manage.pojo.PageResponse;
import com.zrfintech.manage.service.MgMenuService;
import com.zrfintech.manage.util.RequestContextUtil;
import com.zrfintech.manage.vo.response.AmisOption;
import com.zrfintech.manage.vo.response.MenuListResponse;
import com.zrfintech.manage.vo.response.MenuResponse;
import com.zrfintech.service.MgMenuRepo;
import com.zrfintech.service.MgRoleMenuPermissionRepo;
import com.zrfintech.service.MgUserRoleRepo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.zrfintech.manage.constant.CommonConstant.BASE_SCHEMA_API;

@RestController
@RequestMapping("/webapi/menu")
public class MgMenuController {

    @Autowired
    protected MgMenuRepo mgMenuRepo;

    @Autowired
    protected MgUserRoleRepo mgUserRoleRepo;

    @Autowired
    protected MgRoleMenuPermissionRepo mgRoleMenuPermissionRepo;

    @Autowired
    protected MgMenuService menuService;

    @RequestMapping("/simpleList")
    public Object simpleList(BasePageEntity basePageEntity) {
        PageResponse<MgMenu> pageResponse = new PageResponse<MgMenu>();
        Page<MgMenu> pageConfigPage = mgMenuRepo.pageList(basePageEntity);
        pageResponse.setItems(pageConfigPage.getRecords());
        pageResponse.setTotal(pageConfigPage.getTotal());
        return pageResponse;
    }

    @RequestMapping("/upsert")
    public Object upsert(MgMenu mgMenu) {
        menuService.upsert(mgMenu);
        return new JSONObject();
    }


    @RequestMapping("/geMenuList")
    public Object geMenuList() {
        String userCode = RequestContextUtil.getRealUser();
        List<String> roleCodeList = mgUserRoleRepo.getRoleCodeListByUserCode(userCode);
        List<String> menuCodeList = mgRoleMenuPermissionRepo.getMenuCodeListByRoleCodeList(roleCodeList);
        List<MgMenu> menuList = mgMenuRepo.getByMenuCodeList(menuCodeList);

        List<MgMenu> firstMenuList = menuList.stream()
                .filter(item -> StringUtils.isEmpty(item.getParentMenuCode()))
                .collect(Collectors.toList());
        List<MenuListResponse> pages = Lists.newArrayList();
        for (MgMenu mgMenu : firstMenuList) {
            MenuListResponse page = new MenuListResponse();
            MenuResponse menuResponse = new MenuResponse();
            menuResponse.setLabel(mgMenu.getLabel());
            menuResponse.setUrl(mgMenu.getUrl());
            String pageCode = mgMenu.getPageCode();
            if (StringUtils.isNotEmpty(pageCode)) {
                menuResponse.setSchemaApi(String.format(BASE_SCHEMA_API, pageCode));
            }
            menuResponse.setIcon(mgMenu.getIcon());
            menuResponse.setChildren(buildChildrenList(mgMenu.getMenuCode(), menuList));
            page.setChildren(Lists.newArrayList(menuResponse));
            pages.add(page);
        }
        Map result = Maps.newHashMap();
        result.put("pages", pages);
        return result;
    }


    @RequestMapping("/getAllMenuTree")
    public Object getAllMenuTree() {
        List<MgMenu> allMenuList = mgMenuRepo.getAll();
        List<MgMenu> firstMenuList = allMenuList.stream()
                .filter(item -> StringUtils.isEmpty(item.getParentMenuCode()))
                .collect(Collectors.toList());
        List<AmisOption> options = Lists.newArrayList();
        for (MgMenu mgMenu : firstMenuList) {
            AmisOption amisOption = new AmisOption(mgMenu.getLabel(),mgMenu.getMenuCode());
            amisOption.setChildren(buildAmisOptionChildrenList(mgMenu.getMenuCode(), allMenuList));
            options.add(amisOption);
        }
        Map result = Maps.newHashMap();
        result.put("options", options);
        return result;
    }

    private List<MenuResponse> buildChildrenList(String menuCode, List<MgMenu> menuList) {
        List<MenuResponse> menuResponseList = Lists.newArrayList();
        List<MgMenu> sonList = menuList.stream()
                .filter(item -> item.getParentMenuCode().equals(menuCode))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(sonList)) {
            return menuResponseList;
        }
        for (MgMenu mgMenu : sonList) {
            MenuResponse menuResponse = new MenuResponse();
            menuResponse.setLabel(mgMenu.getLabel());
            menuResponse.setUrl(mgMenu.getUrl());
            String pageCode = mgMenu.getPageCode();
            if (StringUtils.isNotEmpty(pageCode)) {
                menuResponse.setSchemaApi(String.format(BASE_SCHEMA_API, pageCode));
            }
            menuResponse.setIcon(mgMenu.getIcon());
            menuResponse.setChildren(buildChildrenList(mgMenu.getMenuCode(), menuList));
            menuResponseList.add(menuResponse);
        }
        return menuResponseList;
    }


    private List<AmisOption> buildAmisOptionChildrenList(String menuCode, List<MgMenu> menuList) {
        List<AmisOption> menuResponseList = Lists.newArrayList();
        List<MgMenu> sonList = menuList.stream()
                .filter(item -> item.getParentMenuCode().equals(menuCode))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(sonList)) {
            return menuResponseList;
        }
        for (MgMenu mgMenu : sonList) {
            AmisOption amisOption = new AmisOption(mgMenu.getLabel(), mgMenu.getMenuCode());
            amisOption.setChildren(buildAmisOptionChildrenList(mgMenu.getMenuCode(), menuList));
            menuResponseList.add(amisOption);
        }
        return menuResponseList;
    }
}
